package com.davidk.survey.client.dataviewer;

import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceIntegerField;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.RichTextEditor;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.layout.VLayout;

public class QuestionItemForm extends VLayout {
//	int ctr = 0;

	public QuestionItemForm() {
		addMember(new Label("<nobr>Question</nobr>"));

		final RichTextEditor questionText = new RichTextEditor();
		questionText.setHeight(155);
		questionText.setOverflow(Overflow.HIDDEN);
		questionText.setCanDragResize(true);
		questionText.setShowEdges(true);
		addMember(questionText);

		RadioGroupItem typeRadios = new RadioGroupItem();
		typeRadios.setTitle("Question Type");
		typeRadios.setValueMap("Free Response", "Multiple Choice", "Regular Expression");
		DynamicForm typeSelect = new DynamicForm();
		typeSelect.setFields(typeRadios);
		addMember(typeSelect);
		DataSource ds = new DataSource() {

			@Override
			protected Object transformRequest(DSRequest dsRequest) {
				return super.transformRequest(dsRequest);
			}

			@Override
			protected void transformResponse(DSResponse response, DSRequest request, Object data) {
				super.transformResponse(response, request, data);
//				Record rec = Record.getOrCreateRef(request.getData());
//				if (rec.getAttributeAsInt("ResponseIdx") == null)
//					rec.setAttribute("ResponseIdx", ctr++);
//				response.setData(new Record[] { rec });
//				System.out.println("Request" + JSON.encode(request.getJsObj()));
//				System.out.println("Response" + JSON.encode(response.getJsObj()));
			}

		};
		DataSourceTextField validResponse = new DataSourceTextField("Response", "Response");
		DataSourceIntegerField validResponseIdx = new DataSourceIntegerField("ResponseIdx", "ResponseIdx");
		validResponseIdx.setPrimaryKey(true);
		validResponseIdx.setDetail(true);
		ds.setFields(validResponseIdx, validResponse);
		ds.setClientOnly(true);

		final ListGrid responseGrid = new ListGrid();
		responseGrid.setDataSource(ds);
		responseGrid.setWidth(550);
		responseGrid.setHeight(224);
		responseGrid.setShowAllRecords(true);
		responseGrid.setBodyOverflow(Overflow.VISIBLE);
		responseGrid.setOverflow(Overflow.VISIBLE);
		responseGrid.setCanEdit(true);
		responseGrid.setCanReorderRecords(true);
		addMember(responseGrid);

		IButton button = new IButton("Edit New");
		button.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				responseGrid.startEditingNew();
			}
		});

		addMember(button);
	}

}
